package com.shujia.hbase;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.mapreduce.HFileOutputFormat;
import org.apache.hadoop.hbase.mapreduce.KeyValueSortReducer;
import org.apache.hadoop.hbase.mapreduce.SimpleTotalOrderPartitioner;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

public class Demo5HdfsToHfile {


    public static class HdfsToHfileMapper extends Mapper<LongWritable, Text, ImmutableBytesWritable, KeyValue> {
        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {

            String[] split = value.toString().split(",");
            String id = split[0];
            String name = split[1];
            String age = split[2];
            String gender = split[3];
            String clazz = split[4];


            //
            ImmutableBytesWritable rowkey = new ImmutableBytesWritable(id.getBytes());

            /**
             * 一个keyvalue代表一个列
             *
             */

            KeyValue keyValue1 = new KeyValue(id.getBytes(), "info".getBytes(), "name".getBytes(), name.getBytes());
            KeyValue keyValue2 = new KeyValue(id.getBytes(), "info".getBytes(), "age".getBytes(), age.getBytes());
            KeyValue keyValue3 = new KeyValue(id.getBytes(), "info".getBytes(), "gender".getBytes(), gender.getBytes());
            KeyValue keyValue4 = new KeyValue(id.getBytes(), "info".getBytes(), "clazz".getBytes(), clazz.getBytes());

            //需要按照rowkey排序
            context.write(rowkey, keyValue1);


            //每个列输出一
            context.write(rowkey, keyValue2);
            context.write(rowkey, keyValue3);
            context.write(rowkey, keyValue4);
        }
    }


    public static void main(String[] args) throws Exception {

        //  HBaseConfiguration.create()  C黄健的配置文件对象  在上传集群运行额时候会自动加载hbse的配置文件
        Configuration conf = HBaseConfiguration.create();

        Job job = Job.getInstance(conf);


        job.setJobName("hdfstoHfile");

        //指定main函数所在的类
        job.setJarByClass(Demo5HdfsToHfile.class);

        job.setMapperClass(HdfsToHfileMapper.class);
        job.setMapOutputKeyClass(ImmutableBytesWritable.class);
        job.setMapOutputValueClass(KeyValue.class);


        /**
         * KeyValueSortReducer  habse提供的对hfile文件进行排序的reduce
         *
         */
        job.setReducerClass(KeyValueSortReducer.class);

        //对数据进行全局排序的自定义分区
        job.setPartitionerClass(SimpleTotalOrderPartitioner.class);


        //  需要先创建表    create 'stu3','info'
        HTable table = new HTable(conf, "stu3");

        HFileOutputFormat.configureIncrementalLoad(job, table);

        //指定数据输入路径
        FileInputFormat.addInputPath(job, new Path("/data/students"));

        //指定生成的hfile文件存放位置
        FileOutputFormat.setOutputPath(job, new Path("/data/hfile"));

        job.waitForCompletion(true);
    }
}
